#include "stm32wig.h"
#include "ui_Stm32wig.h"

Stm32Wig::Stm32Wig(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Stm32Wig)
{
    ui->setupUi(this);

    QFile qss_file(":/qss/style/stm32.qss"); // 使用资源系统来加载，如果是普通路径，请调整
    if (qss_file.open(QFile::ReadOnly)) {
        QString qss_text = QLatin1String(qss_file.readAll());
        this->setStyleSheet(qss_text);
        qss_file.close();
    }

    // 创建 QMovie 对象，并加载 GIF 文件
        movie = new QMovie(":/new/img/ju52c-6umal.gif");
        movie1 = new QMovie(":/new/img/fan_hot.gif");
        movie2 = new QMovie(":/new/img/fan_cold.gif");
        // 设置 QMovie 到 QLabel
        ui->label_11->setMovie(movie);
        ui->label_9->setMovie(movie1);
        ui->label_10->setMovie(movie2);
        // 启动 GIF 动画
        movie->jumpToFrame(0);
        movie1->jumpToFrame(0);
        movie2->jumpToFrame(0);

        connect(ui->motor,SIGNAL(clicked()),this,SLOT(movieToggle()));
        connect(ui->fan,SIGNAL(clicked()),this,SLOT(movieToggle()));
        connect(ui->warm,SIGNAL(clicked()),this,SLOT(movieToggle()));



    connect(ui->fan,SIGNAL(clicked()),this,SLOT(sendJson()));
    connect(ui->warm,SIGNAL(clicked()),this,SLOT(sendJson()));
    connect(ui->led,SIGNAL(clicked()),this,SLOT(sendJson()));
    connect(ui->motor,SIGNAL(clicked()),this,SLOT(sendJson()));
    connect(ui->buzzer,SIGNAL(clicked()),this,SLOT(sendJson()));



        QString qss = "QCheckBox::indicator:unchecked{ \
                       image:url(:/new/img/Toggle,toggle state=off, size=lg, State=hover.png); \
                       } \
                       QCheckBox::indicator:checked { \
                       image: url(:/new/img/Toggle,toggle state=on, size=lg, State=hover.png); \
                       }";

        ui->fan->setStyleSheet(qss);
        ui->fan->setChecked(false);
        ui->warm->setStyleSheet(qss);
        ui->warm->setChecked(false);
        ui->led->setStyleSheet(qss);
        ui->led->setChecked(false);
        ui->motor->setStyleSheet(qss);
        ui->motor->setChecked(false);
        ui->buzzer->setStyleSheet(qss);
        ui->buzzer->setChecked(false);

}

void Stm32Wig::movieToggle()
{
    QCheckBox *checkbox = qobject_cast<QCheckBox*>(sender());
    if(checkbox->objectName()=="motor"){
        if(ui->motor->isChecked()==true){
            movie->start();
        }
        else if(ui->motor->isChecked()==false){
            movie->stop();
        }
    }
    if(checkbox->objectName()=="fan"){
        if(ui->fan->isChecked()==true){
            movie1->start();
        }
        else if(ui->fan->isChecked()==false){
            movie1->stop();
        }
    }
    if(checkbox->objectName()=="warm"){
        if(ui->warm->isChecked()==true){
            movie2->start();
        }
        else if(ui->warm->isChecked()==false){
            movie2->stop();
        }
    }
}
Stm32Wig::~Stm32Wig()
{
    delete ui;
}


void Stm32Wig::dataUpSlot(QByteArray jsonString)
{

    // 将 QByteArray 转换为 QJsonDocument
    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString);



       // 检查 JSON 是否有效
       if (!jsonDoc.isNull()) {
           // 获取 JSON 对象
           QJsonObject jsonObj = jsonDoc.object();

           // 解析基本数据
           if(jsonObj.contains("type"))
           {
              QJsonValue value = jsonObj.value("type");
              if(value != 1)
              {
                  return;
              }
           }
           int type = jsonObj.value("type").toInt();
           int result = jsonObj.value("result").toInt();

           qDebug() << "Type:" << type;
           qDebug() << "Result:" << result;

           // 解析数据数组
           QJsonArray dataArray = jsonObj.value("data").toArray();

           for (const QJsonValue &value : dataArray) {
               QJsonObject dataObj = value.toObject(); // 将 QJsonValue 转换为 QJsonObject
               int key = dataObj.value("key").toInt();
               QString val = dataObj.value("val").toString();
               qDebug() << "Key:" << key << ", Value:" << val;
               if(key ==301){
                   ui->hum_val->setText(val+"°C");
               }
               if(key ==302){
                   ui->tem_val->setText(val+"%");
               }
               if(key ==303){
                   if(val=="1"){
                        ui->fan->setChecked(1);
                        movie2->start();
                   }
                   else{
                       ui->fan->setChecked(0);
                       movie2->stop();

                   }
               }
               if(key ==304){
                   if(val=="1"){
                        ui->warm->setChecked(1);
                        movie1->start();
                   }
                   else{
                       ui->warm->setChecked(0);
                       movie1->stop();
                   }

               }
               if(key ==305){
                   if(val=="1"){
                        ui->led->setChecked(1);
                   }
                   else{
                       ui->led->setChecked(0);
                   }
               }
               if(key ==306){
                   if(val=="1"){
                        ui->motor->setChecked(1);
                        movie->start();
                   }
                   else{
                       ui->motor->setChecked(0);
                       movie->stop();
                   }
               }
               if(key ==307){
                   if(val=="1"){
                        ui->buzzer->setChecked(1);
                   }
                   else{
                       ui->buzzer->setChecked(0);
                   }
               }
           }
       } else {
           qWarning() << "Failed to parse JSON.";
       }


}

void Stm32Wig::sendJson()
{
    // 获取信号发射者对象
        QCheckBox *checkbox = qobject_cast<QCheckBox*>(sender());

        // 创建 JSON 对象
        QJsonObject jsonObj;
        jsonObj["type"] = 2; // 指令类型
        QJsonObject dataObj;
        int key;
        if(checkbox->objectName()=="fan"){
            key =303;
        }else if(checkbox->objectName()=="warm"){
            key =304;
        }else if(checkbox->objectName()=="led"){
            key =305;
        }else if(checkbox->objectName()=="motor"){
            key =306;
        }else if(checkbox->objectName()=="buzzer"){
            key =307;
        }
        dataObj["key"] = key; // 数据点唯一标识

        // 根据 QCheckBox 状态设置 val 值
        QString val = (checkbox->isChecked() ? "1" : "0");
        dataObj["val"] = val; // 数据点键值
        jsonObj["data"] = dataObj;

        // 将 JSON 对象转换为字符串
        QJsonDocument doc(jsonObj);
        QByteArray jsonData = doc.toJson(QJsonDocument::Compact);

        // 发送 JSON 数据
        qDebug() << "Sending JSON data:" << jsonData;
        emit sendmessage(jsonData);
}


